<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
/**
 * 会员组管理
 *
 * @icon fa fa-users
 */
class Group extends Backend
{

    /**
     * @var \app\admin\model\UserGroup
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('UserGroup');
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post('row/a');
            if(empty($data['name'])){
                $this->error('请输入组名称');
            }
            Db::startTrans();
            try{
                $data['wechat_id'] = $this->wechat_id;
                $data['createtime'] = time();
                $group_id = $this->model->insert($data,true,true);
                $result = $this->weobj()->createGroup($data['name']);
                if(!$result){
                    Db::rollback();
                    $this->error("保存失败");
                }
            }catch(\Exception $e){
                Db::rollback();
                $this->error("保存失败");
            }
            $this->model->where("id",$group_id)->update(['group_id'=>$result['group']['id'],'updatetime'=>time()]);
            Db::commit();
            $this->success('数据保存成功');
        }
        //$nodeList = \app\admin\model\UserRule::getTreeList();
        //$this->assign("nodeList", $nodeList);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $data = $this->request->post('row/a');
            if(empty($data['name'])){
                $this->error('请输入组名称');
            }
            Db::startTrans();
            try{
                $row->updatetime = time();
                $row->name = $data['name'];
                $row->save();
                $result = $this->weobj()->updateGroup($row['group_id'],$data['name']);
                if(!$result){
                    Db::rollback();
                    $this->error("保存失败");
                }
            }catch(\Exception $e){
                Db::rollback();
                $this->error("保存失败");
            }
            Db::commit();
            $this->success('数据保存成功');
        }
        $this->assign("row", $row);

        return $this->view->fetch();
    }
    public function del($ids=null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        Db::startTrans();
        try{
            $row->delete();
            $result = $this->weobj()->deleteGroup($row['group_id']);
            if(!$result){
                Db::rollback();
                $this->error("删除数据失败");
            }
        }catch(\Exception $e){
            Db::rollback();
            $this->error("删除数据失败");
        }
        Db::commit();
        $this->success('数据删除成功');
    }

}
